package io.lckgroup.modules.lck.controller.app;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.lckgroup.common.utils.PageUtils;
import io.lckgroup.common.utils.R;
import io.lckgroup.modules.lck.annotation.AppLogin;
import io.lckgroup.modules.lck.annotation.AppLoginUser;
import io.lckgroup.modules.lck.entity.ShopFoodEntity;
import io.lckgroup.modules.lck.entity.ShopFoodSpecEntity;
import io.lckgroup.modules.lck.entity.ShopMerchantInfoEntity;
import io.lckgroup.modules.lck.entity.appvo.ShopFoodEntityVo;
import io.lckgroup.modules.lck.form.ShopFoodEntityForm;
import io.lckgroup.modules.lck.service.ShopFoodService;
import io.lckgroup.modules.lck.service.ShopFoodSpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * APP Do菜品表
 *
 * @author srq
 * @email siruiqi@siruiqi.com
 * @date 2020-2-4 11:43:59
 */
@RestController
@RequestMapping("/app/shopFood")
@Api("APP菜品管理接口")
public class AppShopFoodController {

    @Autowired
    private ShopFoodService shopFoodService;

    @Autowired
    private ShopFoodSpecService shopFoodSpecService;

    /**
     * 添加修改菜品信息
     *
     * @param entity
     * @param sm
     * @return R
     */
    @AppLogin
    @PostMapping("addOrUpdate")
    @ApiOperation("添加修改菜品信息")
    public R addOrUpdateFood(@RequestBody ShopFoodEntityForm entity, @AppLoginUser ShopMerchantInfoEntity sm) {

        //判断是否有重复数据
        String newName = entity.getGoodsName().replaceAll(" ", "");
        entity.setGoodsName(newName);
        entity.setIsUpper(0);//新增修改时默认下架
        entity.setIsDelete(0);//未删除
        if (1 == entity.getIsSpecs()) {//有规格
            entity.setPrice(new BigDecimal(0));
        }

        if (null == entity.getId()) {
            //获取商户当前登录用户的商户信息
            ShopMerchantInfoEntity merchant = sm;
            entity.setMerchantId(merchant.getId());
            //通过菜品名称,商家id查询数据,菜品分类id
            ShopFoodEntity query = new ShopFoodEntity();
            query.setGoodsName(newName);
            query.setMerchantId(sm.getId());
            query.setGoodsTypeId(entity.getGoodsTypeId());
            query.setIsDelete(0);
            Wrapper<ShopFoodEntity> queryWrapper = new QueryWrapper<>(query);
            List<ShopFoodEntity> list = shopFoodService.list(queryWrapper);
            if (null != list && list.size() > 0) {
                return R.error(-1, "菜品已经存在");
            }
        } else {//执行修改方法
            entity.setGmtModified(new Date());
            //通过菜品名称,商家id查询除自己以外的数据
            Wrapper<ShopFoodEntity> queryWrapper =
                    new QueryWrapper<ShopFoodEntity>()
                            .eq("merchant_id", sm.getId())
                            .eq("food_name", newName)
                            .eq("food_type_id", entity.getGoodsTypeId())
                            .eq("is_delete", 0)
                            .notIn("id", entity.getId());
            List<ShopFoodEntity> list = shopFoodService.list(queryWrapper);
            if (null != list && list.size() > 0) {
                return R.error(-1, "菜品已经存在");
            }
        }
        //调用方法操作数据
        boolean r = shopFoodService.myAddOrUpdate(entity);

        if (r) {
            return R.ok().put("msg", "操作成功");
        } else {
            return R.error(-1, "操作失败");
        }
    }

    /**
     * 获取菜品的列表
     *
     * @param params
     * @param sm
     * @return R
     */
    @AppLogin
    @GetMapping("getList")
    @ApiOperation("获取菜品的列表")
    public R getList(@RequestParam Map<String, Object> params, @AppLoginUser ShopMerchantInfoEntity sm) {
        //获取商家信息参数
        ShopMerchantInfoEntity m = sm;
        ShopFoodEntity shopFoodEntity = new ShopFoodEntity();
        shopFoodEntity.setMerchantId(m.getId());
        params.put("merchantId", m.getId());
        params.put("isDelete", 0);

        //获取分页数据
        IPage<ShopFoodEntityVo> r = shopFoodService.queryPageByDefine(params);

        PageUtils pageUtils = new PageUtils(r.getRecords(), (int) r.getTotal(), (int) r.getSize(), (int) r.getCurrent());
        return R.ok().put("results", pageUtils);
    }

    /**
     * 通过主键获取菜品信息
     *
     * @param id
     * @return R
     */
    @AppLogin
    @GetMapping("/getShopFoodById/{id}")
    @ApiOperation("通过主键获取菜品信息")
    public R getFoodTypeById(@PathVariable("id") String id) {
        ShopFoodEntity shopFoodEntity = shopFoodService.getById(id);
        ShopFoodSpecEntity entity = new ShopFoodSpecEntity();
        entity.setGoodsId(Long.valueOf(id));
        Wrapper<ShopFoodSpecEntity> queryWrapper = new QueryWrapper<ShopFoodSpecEntity>(entity);
        List<ShopFoodSpecEntity> list = shopFoodSpecService.list(queryWrapper);

        return R.ok().put("results", shopFoodEntity).put("list", list);
    }

    /**
     * 通过主键删除菜品分类信息
     *
     * @param id
     * @return R
     */
    @AppLogin
    @GetMapping("/deleteShopFoodById/{id}")
    @ApiOperation("通过主键删除菜品分类信息")
    public R deleteShopFoodById(@PathVariable("id") String id) {
        boolean result = shopFoodService.myDeleteShopFoodById(id);
        if (result) {
            return R.ok().put("msg", "操作成功");
        } else {
            return R.error(-1, "操作失败,只能删除没有菜品的分类");
        }

    }


    /**
     * 处理菜品上架的接口
     *
     * @param entity
     * @return R
     */
    @AppLogin
    @PostMapping("/dealIsUpper")
    @ApiOperation("处理菜品上架的接口")
    @CacheEvict(value = {"findFoodList", "selectPageByAuditState"}, allEntries = true)
    public R dealIsUpper(@RequestBody ShopFoodEntity entity) {
        if (0 == entity.getIsUpper()) {
            entity.setIsUpper(1);
        } else if (1 == entity.getIsUpper()) {
            entity.setIsUpper(0);
        }
        boolean result = shopFoodService.updateById(entity);
        if (result) {
            return R.ok().put("msg", "操作成功");
        } else {
            return R.error(-1, "操作失败");
        }

    }


}
